package com.luban.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import org.junit.Test;

/**
 * @ClassName TestBytebuf
 * @Description 498982703@qq.com
 * @Author gary
 * @Date 2021/2/19 15:33
 * @Version 1.0
 */
public class TestBytebuf {
    public static void main(String[] args) {
        ByteBuf buffer = Unpooled.buffer(10);
        System.out.println("byteBuf:" + buffer.toString());
        for (int i = 0; i < 8; i++) {
            buffer.writeByte(i);
        }
        System.out.println(buffer);
        for (int i = 0; i < 5; i++) {
            System.out.println(buffer.getByte(i));
        }
        for (int i = 0; i < 5; i++) {
            System.out.println(buffer.readByte());
        }
        System.out.println(buffer);

    }

    @Test
    public void test2() {
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello,zhuge", CharsetUtil.UTF_8);
        if (byteBuf.hasArray()) {
            byte[] content = byteBuf.array();
            System.out.println(new String(content, CharsetUtil.UTF_8));
            System.out.println(byteBuf);
            System.out.println(byteBuf.getByte(0));
            int readLen = byteBuf.readableBytes();
            System.out.println("readlen:"+readLen);
            for (int i = 0; i < readLen; i++) {
                System.out.println((char) byteBuf.getByte(i));
            }
            System.out.println(byteBuf.getCharSequence(0, 6, CharsetUtil.UTF_8));
            System.out.println(byteBuf.getCharSequence(6, 6, CharsetUtil.UTF_8));

        }
    }
}
